package com.cn.ccp.action;

import java.io.File;
import java.util.List;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.cn.ccp.bean.CourseContentBean;
import com.cn.ccp.bean.CourseContentManageBean;
import com.cn.ccp.bean.FilePathBean;
import com.cn.ccp.bean.PageItemBean;
import com.cn.ccp.service.classschedule.ClassScheduleDetailService;
import com.cn.ccp.service.classschedule.ClassScheduleService;
import com.cn.ccp.service.classschedule.ClassScheduleSettingService;
import com.cn.ccp.service.classschedule.TermSettingService;
import com.cn.ccp.service.course.CourseContentService;
import com.cn.ccp.service.course.CourseForumService;
import com.cn.ccp.service.course.CourseInvestigationService;
import com.cn.ccp.service.course.CourseIssueService;
import com.cn.ccp.service.course.CourseService;
import com.cn.ccp.service.course.FilePathService;
import com.cn.ccp.service.course.SelectCourseService;
import com.cn.ccp.service.course.TrainingService;
import com.cn.ccp.service.faculty.FacultyService;
import com.cn.ccp.service.faculty.GradeService;
import com.cn.ccp.service.faculty.SpecialtyService;
import com.cn.ccp.service.faculty.TeachingGroupService;
import com.cn.ccp.service.onlineEdu.OnlineExamService;
import com.cn.ccp.service.resourse.ResourseService;
import com.cn.ccp.service.system.MenuManageService;
import com.cn.ccp.service.system.MenuService;
import com.cn.ccp.service.system.RoleService;
import com.cn.ccp.service.system.StudentService;
import com.cn.ccp.service.system.TeacherService;
import com.cn.ccp.service.system.UserService;
import com.cn.ccp.util.CCPContants;
import com.cn.ccp.util.MsgConstants;

public class BaseAction extends PageItemBean {
	
	private static BaseAction INSTANCE = null ;

	public String SUCCESS = "success";
	public String ADD = "add";
	public String UPDATE = "update";
	public String DELETE = "delete";
	public String MAIN = "main";;
	public String COURSECONTENT = "course_content";
	public String EDIT = "edit";
	public String FAILED = "failed";
	public String SWF_FILE = "swfFileUrl" ;
	public String DEFAULT_SWF_URL = "swfUrl";
	public String FLV_URL = "flvUrl";		
	public String DEFAULT_VEDIO_FILE = "defaultVedioUrl";
	public String VEDIO_FILE = "vedioUrl";
	public String UPDATE_RELEASE_APPROVAL = "updata_release_approval";
	
	public String UPDATE_ROLE_MENU = "updateRoleMenu";
	
	public String labelBtnAdd,labelBtnEdit,labelBtnSubmit,labelBtnReset,labelBtnDelete;
	
	public BaseAction(){
		labelBtnAdd = getMessages(MsgConstants.BTN_ADD);
		labelBtnEdit = getMessages(MsgConstants.BTN_EDIT);
		labelBtnSubmit = getMessages(MsgConstants.BTN_SUBMIT);
		labelBtnReset = getMessages(MsgConstants.BTN_RESET);
		labelBtnDelete = getMessages(MsgConstants.BTN_DELETE);
	}
	
	public static BaseAction getInstance(){
		if(INSTANCE == null)
			INSTANCE = new BaseAction();
		return INSTANCE ;
	}
	
	@Override
	public void getpagedetail() {
		// TODO Auto-generated method stub

	}

	public RoleService getRoleService() {
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		RoleService roleService = (RoleService) fcontextutils.getBean("roleService");
		return roleService;
	}

	public MenuService getMenuService() {
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		MenuService menuService = (MenuService) fcontextutils
				.getBean("menuService");
		return menuService;
	}
	
	public UserService getUserService() {
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		UserService userService = (UserService) fcontextutils.getBean("userService");
		return userService;
	}
	
	public FacultyService getFacultyService() {
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		FacultyService facultyService = (FacultyService) fcontextutils.getBean("facultyService");
		return facultyService;
	}
	
	public SpecialtyService getSpecialtyService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		SpecialtyService specialtyService = (SpecialtyService) fcontextutils.getBean("specialtyService");
		return specialtyService ;
	}
	
	public TermSettingService getTermSettingService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		TermSettingService termSettingService = (TermSettingService) fcontextutils.getBean("termSettingService");
		return termSettingService ;
	}
	
	public ClassScheduleSettingService getScheduleSettingService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		ClassScheduleSettingService scheduleSettingService = (ClassScheduleSettingService) fcontextutils.getBean("ClassScheduleSettingService");
		return scheduleSettingService ;
	}
	
	public MenuManageService getMenuManageService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		MenuManageService menuManageService = (MenuManageService) fcontextutils.getBean("menuManageService");
		return menuManageService ;
	}
	public TeacherService getTeacherService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		TeacherService teacherService = (TeacherService) fcontextutils.getBean("teacherService");
		return teacherService ;
	}
	public StudentService getStudentService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		StudentService studentService = (StudentService) fcontextutils.getBean("studentService");
		return studentService ;
	}
	
	/**
	 * 返回课程表service
	 * @return
	 */
	public ClassScheduleService getClassScheduleService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		ClassScheduleService classScheduleService = (ClassScheduleService) fcontextutils.getBean("classScheduleService");
		return classScheduleService ;
	}
	
	public ClassScheduleDetailService getClassScheduleDetailService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		ClassScheduleDetailService classScheduleDetailService = (ClassScheduleDetailService) fcontextutils.getBean("classScheduleDetailService");
		return classScheduleDetailService ;
	}
	
	/**
	 * 返回班级管理service
	 * @return
	 */
	public GradeService getGradeService(){
		WebApplicationContext fcontextutils = FacesContextUtils
				.getWebApplicationContext(FacesContext.getCurrentInstance());
		GradeService gradeService = (GradeService) fcontextutils
				.getBean("gradeService");
		return gradeService;
	}
	
	public CourseService getCourseService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		CourseService courseService = (CourseService) fcontextutils.getBean("courseService");
		return courseService;
	}
	
	public SelectCourseService getSelectCourseService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		SelectCourseService courseService = (SelectCourseService) fcontextutils.getBean("selectCourseService");
		return courseService;
	}
	
	public CourseContentService getCourseContentService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		CourseContentService courseContentService = (CourseContentService) fcontextutils.getBean("courseContentService");
		return courseContentService;
	}
	
	public TrainingService getTrainingService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		TrainingService trainingService = (TrainingService) fcontextutils.getBean("trainingService");
		return trainingService;
	}
	
	public CourseIssueService getCourseIssueService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		CourseIssueService courseIssueService = (CourseIssueService) fcontextutils.getBean("courseIssueService");
		return courseIssueService;
	}
	
	public OnlineExamService getOnlineExamService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		OnlineExamService onlineExamService = (OnlineExamService) fcontextutils.getBean("onlineExamService");
		return onlineExamService;
	}
	
	public TeachingGroupService getTeachingGroupService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		TeachingGroupService teachingGroupService = (TeachingGroupService) fcontextutils.getBean("teachingGroupService");
		return teachingGroupService;
	}
	
	public ResourseService getResourseService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		ResourseService resourseService = (ResourseService) fcontextutils.getBean("resourseService");
		return resourseService;
	}
	
	public FilePathService getFilePathService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		FilePathService filePathService = (FilePathService) fcontextutils.getBean("filePathService");
		return filePathService;
	}
	
	public CourseInvestigationService getCourseInvestigationService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		CourseInvestigationService courseInvestigationService = (CourseInvestigationService) fcontextutils.getBean("courseInvestigationService");
		return courseInvestigationService;
	}
	
	public CourseForumService getCourseForumService(){
		WebApplicationContext fcontextutils = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
		CourseForumService courseForumService = (CourseForumService) fcontextutils.getBean("courseForumService");
		return courseForumService;
	}
	
	
	
	/**
	 * 在页面上显示提示信息
	 * 
	 * @param id
	 *            对应页面上的具体某个目标点
	 * @param severity信息等级
	 * @param summary总结
	 * @param detail细节
	 */
	public void showMsg(String id, String key) {
		String formid = "";
		if(key.equals("formid"))
			formid = key ;
		else 
			formid = "formid:"+id ;
		FacesContext.getCurrentInstance().addMessage(formid, new FacesMessage(FacesMessage.SEVERITY_ERROR, getMessages(MsgConstants.MESSAGES_ERROR),getMessages(key)));
	}

	/**
	 * 获取国际化信息
	 * 
	 * @param key
	 * @return
	 */
	public String getMessages(String key) {
		FacesContext context = FacesContext.getCurrentInstance();
		String bundleName = context.getApplication().getMessageBundle();
		ResourceBundle messageBundle = ResourceBundle.getBundle(bundleName,
				context.getViewRoot().getLocale());
		try {
			return messageBundle.getString(key);
		} catch (Exception e) {
			return "";
		}
	}

	public String getLabelBtnAdd() {
		return labelBtnAdd;
	}

	public String getLabelBtnEdit() {
		return labelBtnEdit;
	}

	public String getLabelBtnSubmit() {
		return labelBtnSubmit;
	}

	public String getLabelBtnReset() {
		return labelBtnReset;
	}

	public String getLabelBtnDelete() {
		return labelBtnDelete;
	}
	
	//////////////////////////
	private String getFilePath(){
		FilePathBean path = getFilePathService().getFlagBean();
		String savePath = CCPContants.SYS_PATH+"files"+File.separator ;
		if(path != null){
			savePath = path.getPath() ;
		}
		return savePath;
	}
	
	/**
	 * 创建小节信息
	 * @param b
	 * @return
	 */
	public final CourseContentManageBean addSecondLevel(CourseContentBean b,boolean showDelete){
		CourseContentManageBean chaperBean = new CourseContentManageBean(); 
		chaperBean.setChapter_number(b.getChapter_number());
		chaperBean.setXtitle(""+b.getId(), b.getChapter_title() ,b.getChapter_number(),showDelete);
		chaperBean.getCcmList().add(b);
		return chaperBean;
	}
	
	
	/**
	 * 添加章节信息
	 * @param b
	 * @return
	 */
	public final CourseContentManageBean addFirstLevel(CourseContentBean b,List<CourseContentManageBean> courseChapterList,boolean showDelete){
		CourseContentManageBean chaperBean = new CourseContentManageBean(); 
		chaperBean.setChapter_number(b.getChapter_number());
		chaperBean.setTitle1(""+b.getId() ,b.getChapter_title(), courseChapterList.size() ,b.getChapter_number(),showDelete);
		chaperBean.setTitle2(courseChapterList.size() );
		courseChapterList.add(chaperBean);
		//兼容以前的课程内容,章节下面的课程内容
		if(b.getSection_file_name() != null && !"".equalsIgnoreCase(b.getSection_file_name())){
			chaperBean.getCcList().add(b);
		}
		return chaperBean;
	}
	
}
